#include <iostream>
#include <fstream>
#include <string>
#include <set>
#include "BiGraph.h"

using namespace std;

int readGraph(BiGraph<int>& graph, int& length) {
    ifstream input("in.txt", ifstream::in);
    if (!input.is_open()) {
        cout<<"No input file!\n";
        system("pause");
        exit(1);
    }
    string strToFind;
    int count = 0;

    input>>strToFind;
    input>>count;

    int findStrLength = strToFind.size();
    string in;
    for (int i = 1; i <= count; ++i) {
        input>>in;
        graph.add_double_edge(0, findStrLength + i, 1);

        for (size_t y = 0 ; y < in.size(); ++y) {
            for (size_t u = 0 ; u < strToFind.size(); ++u) {
                if (in[y] == strToFind[u]) {
                    graph.add_double_edge(findStrLength + i, u + 1, 1);
                }
            }
        }
    }

    for (size_t i = 1; i <= strToFind.size(); ++i) {
        graph.add_double_edge(i, findStrLength + count + 1, 1);
    }

    length = findStrLength;
    return findStrLength + count + 1;
}

int main() {
    BiGraph<int> graph(true);
    int length = 0;
    int last = readGraph(graph, length);

    graph.Ford_Fulkerson(0,last);

    double summ = 0;
    set<int> cubes;
    for (BiGraph<int>::In_Edge_Iterator i = graph.get_vertex(last)->in_edge_begin(); i != graph.get_vertex(last)->in_edge_end(); ++i) {
        if (i->f > 0) {
            summ += i->f;
            int cube = i->get_vertices().first->get_name();
            for (BiGraph<int>::In_Edge_Iterator y = graph.get_vertex(cube)->in_edge_begin(); y != graph.get_vertex(cube)->in_edge_end(); ++y) {
                if (y->f > 0) cubes.insert(y->get_vertices().first->get_name());
            }
        }
    }

    if (summ == length) {
        cout<<"YES\n";
        for (set<int>::iterator i = cubes.begin(); i != cubes.end(); ++i) {
            cout<<*i - length<<" ";
        }
        cout<<endl;
    }
    else { cout<<"NO\n"; }

    system("pause");
    return 0;
}
